package org.xg.study.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Properties;

/**
 * @author CMG
 * @version 1.0
 * @description
 * @cLassName ReadConfig
 * @date 2021/9/23 16:08
 **/
@WebServlet("/dbConfig")
public class ReadConfig extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改request缓冲区的字符集为UTF-8
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter printWriter = resp.getWriter();
        ServletContext context = getServletContext();
        InputStream ins = context.getResourceAsStream("/WEB-INF/classes/db" +
                ".properties");
        BufferedReader bufferedReader =
                new BufferedReader(new InputStreamReader(ins, "utf-8"));
        Properties prop = new Properties();
        prop.load(bufferedReader);
        String name = prop.getProperty("name");
        String url = prop.getProperty("url");
        String desc = prop.getProperty("desc");
        printWriter.write("用户名：" + name + "<br/>" + "地址：" + url + "<br/>" +
                "描述：" + desc + "<br/>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
